﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GeneratorLib.DB;
using GeneratorLib.DB.Connectios;
using GeneratorLib.DB.Schemas;

namespace DBExplorer
{

    [Serializable]
    public class ConnectionInfos
    {
        private static string savePath= System.IO.Path.Combine(GlobalValues.CurrentPath, "DBCon.dbc");
        public List<IConnection> Connections { get; set; }

        public ConnectionInfos()
        {
            Connections = new List<IConnection>();
        }

        public void AddItem(IConnection info)
        {

            Connections.Add(info);
            Save();
        }

        public void RemoveItem(IConnection info)
        {
            Connections.Remove(info);
            Save();
        }

        public void Save()
        {
            Save(this);
        }

        public static void Save(ConnectionInfos infos)
        {

            string pathtmp = System.IO.Path.Combine(GlobalValues.CurrentPath, "DBConTmp.dbc");
            string path = savePath;// System.IO.Path.Combine(GlobalValues.CurrentPath, "DBCon.dbc");

            if (System.IO.File.Exists(pathtmp))
            {
                System.IO.File.Delete(pathtmp);
            }

            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            using (System.IO.FileStream fs = new System.IO.FileStream(pathtmp, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write))
            {
                formatter.Serialize(fs, infos);
            }

            System.IO.File.Copy(pathtmp, path, true);
        }

        public static ConnectionInfos Read()
        {
            string path = savePath;// System.IO.Path.Combine(GlobalValues.CurrentPath, "DBCon.dbc");

            if (!System.IO.File.Exists(path))
            {
                return new ConnectionInfos();
            }

            ConnectionInfos infos = null;
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                try
                {
                    infos = (ConnectionInfos)formatter.Deserialize(fs);
                }
                catch
                {
                    infos = new ConnectionInfos();
                }
            }
            return infos;
        }

        public override int GetHashCode()
        {
            return savePath.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            ConnectionInfos obj2 = obj as ConnectionInfos;
            if (obj2 == null)
            {
                return false;
            }

            return true;
        }

    }

}
